package cn.db101.view;

import cn.db101.dao.readerDao;
import cn.db101.model.reader;
import cn.db101.util.DbUtil;
import cn.db101.util.StringUtil;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;

public class registerFrame {
	JFrame jf = new JFrame();
	private JPanel contentPane;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private JPasswordField password_Field;
	private JPasswordField repeat_password_Field;
	private JTextField account_textField;
	private JTextField name_textField;
	private	JRadioButton man_radioButton;
	private JRadioButton woman_radioButton;
	private DbUtil dbUtil=new DbUtil();
	/**
	 *  private  DbUtil dbUtil �ᱨ��java.lang.NullPointerException
	 *  private DbUtil dbUtil=new DbUtil();���ᱨ��
	 *  ����ԭ�򣺽ӿ����͵Ķ���û���þ�������ʼ��
	 */
	private JTextField departmentField;


	/**
	 * Create the frame.
	 */
	public  void init() {
		jf.setTitle("\u767B\u5F55主界面");
		
		jf.setIconImage(Toolkit.getDefaultToolkit().getImage(registerFrame.class.getResource("/icon/Libraries_64px_1176502_easyicon.net.png")));
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf.setBounds(100, 100, 1278, 805);
		
		JMenuBar menuBar = new JMenuBar();
		jf.setJMenuBar(menuBar);
		
		JMenu m1 = new JMenu("主界面");
		m1.setIcon(new ImageIcon(registerFrame.class.getResource("/icon/线性图书.png")));
		menuBar.add(m1);
		
		JMenu m2 = new JMenu("图书管理规定");
		m2.setIcon(new ImageIcon(registerFrame.class.getResource("/icon/线性老师.png")));
	
		
		menuBar.add(m2);
		
		JMenu m3 = new JMenu(" 图书查询");
		m3.setIcon(new ImageIcon(registerFrame.class.getResource("/icon/线性本子vg.png")));
		menuBar.add(m3);
		
		JMenu m4 = new JMenu(" \u6CE8\u518C");
		m4.setIcon(new ImageIcon(registerFrame.class.getResource("/icon/线性铅笔.png")));
		menuBar.add(m4);
		
		JMenu m5 = new JMenu("\u767B\u5F55");
		m5.setIcon(new ImageIcon(registerFrame.class.getResource("/icon/线性双指点击.png")));
		menuBar.add(m5);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		jf.setContentPane(contentPane);
		
		JLayeredPane layeredPane = new JLayeredPane();
		contentPane.add(layeredPane, BorderLayout.CENTER);
		
		JLayeredPane layeredPane_1 = new JLayeredPane();
		layeredPane_1.setBounds(733, 81, 280, 442);
		layeredPane.add(layeredPane_1);
		
		JLabel account_label = new JLabel("\u8D26    \u53F7\uFF1A");
		account_label.setFont(new Font("微软雅黑", Font.PLAIN, 17));
		account_label.setBounds(40, 33, 72, 18);
		layeredPane_1.add(account_label);
		
		JLabel name_label = new JLabel("\u59D3    \u540D\uFF1A");
		name_label.setFont(new Font("微软雅黑", Font.PLAIN, 17));
		name_label.setBounds(40, 93, 72, 18);
		layeredPane_1.add(name_label);
		
		JLabel gender_label = new JLabel("\u6027    \u522B\uFF1A");
		gender_label.setFont(new Font("微软雅黑", Font.PLAIN, 17));
		gender_label.setBounds(40, 152, 72, 18);
		layeredPane_1.add(gender_label);
		
		 man_radioButton = new JRadioButton("\u7537");
		buttonGroup.add(man_radioButton);
		man_radioButton.setSelected(true);
		man_radioButton.setBounds(126, 150, 43, 27);
		layeredPane_1.add(man_radioButton);
		
		 woman_radioButton = new JRadioButton("\u5973");
		buttonGroup.add(woman_radioButton);
		woman_radioButton.setBounds(193, 150, 53, 27);
		layeredPane_1.add(woman_radioButton);
		
		password_Field = new JPasswordField();
		password_Field.setBounds(126, 267, 120, 24);
		layeredPane_1.add(password_Field);
		
		JLabel gender_label_1 = new JLabel("\u5BC6    \u7801\uFF1A");
		gender_label_1.setFont(new Font("微软雅黑", Font.PLAIN, 17));
		gender_label_1.setBounds(40, 268, 72, 18);
		layeredPane_1.add(gender_label_1);
		
		JLabel gender_label_1_1 = new JLabel("\u786E\u8BA4\u5BC6\u7801\uFF1A");
		gender_label_1_1.setFont(new Font("微软雅黑", Font.PLAIN, 17));
		gender_label_1_1.setBounds(40, 332, 90, 18);
		layeredPane_1.add(gender_label_1_1);
		
		repeat_password_Field = new JPasswordField();
		repeat_password_Field.setBounds(126, 331, 120, 24);
		layeredPane_1.add(repeat_password_Field);
		
		JButton submit_Button = new JButton("\u63D0\u4EA4");
		submit_Button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				register(e);
			}
		});
		submit_Button.setIcon(new ImageIcon(registerFrame.class.getResource("/icon/login_16px_1207631_easyicon.net.png")));
		submit_Button.setBounds(133, 388, 113, 27);
		layeredPane_1.add(submit_Button);
		
		account_textField = new JTextField();
		account_textField.setBounds(126, 32, 120, 24);
		layeredPane_1.add(account_textField);
		account_textField.setColumns(10);
		
		name_textField = new JTextField();
		name_textField.setBounds(125, 92, 121, 24);
		layeredPane_1.add(name_textField);
		name_textField.setColumns(10);
		
		JLabel gender_label_1_1_1 = new JLabel("\u6240\u5728\u9662\u7CFB\uFF1A");
		gender_label_1_1_1.setFont(new Font("微软雅黑", Font.PLAIN, 17));
		gender_label_1_1_1.setBounds(40, 215, 90, 18);
		layeredPane_1.add(gender_label_1_1_1);
		
		departmentField = new JTextField();
		departmentField.setColumns(10);
		departmentField.setBounds(126, 209, 121, 24);
		layeredPane_1.add(departmentField);
		
		JLabel label = new JLabel("");
		label.setIcon(new ImageIcon(registerFrame.class.getResource("/img/register.png")));
		label.setBounds(0, 0, 1250, 722);
		layeredPane.add(label);
		
		
		m1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {

				//���ٵ�ǰҳ��
				closeThis();
				//���´���
				new mainFrame().init();
			}
		});	
		m2.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {

				//���ٵ�ǰҳ��
				closeThis();
				//���´���
				new regulationFrame().init();
			}
		});	
		m3.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {

				//���ٵ�ǰҳ��
				closeThis();
				//���´���
				new searchFrame().init();
			}
		});	
	
		m5.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {

				//���ٵ�ǰҳ��
				closeThis();
				//���´���
				new loginFrame().init();
			}
		});	
		//����JFrame������ʾ
		jf.setLocationRelativeTo(null);
		//���ô���ɼ�
		jf.setVisible(true);	
	}
	/**
	 * ע���¼�����
	 * @param evt
	 */
	public void register(ActionEvent evt) {
		String account =this.account_textField.getText();
		if(StringUtil.isEmpty(account)) {
			JOptionPane.showMessageDialog(null, "�˺Ų���Ϊ��");
			return;
		}
		String name=this.name_textField.getText();
		if(StringUtil.isEmpty(name)) {
			JOptionPane.showMessageDialog(null, "��������Ϊ��");
			return;
		}
		String password=new String(this.password_Field.getPassword());
		if(StringUtil.isEmpty(password)) {
			JOptionPane.showMessageDialog(null, "���벻��Ϊ��");
			return;
		}
		String repeat_password=new String(this.repeat_password_Field.getPassword());
		if(StringUtil.isEmpty(repeat_password)) {
			JOptionPane.showMessageDialog(null, "�ظ����벻��Ϊ��");
			return;
		}
		String gender="";
		if(man_radioButton.isSelected())
		{
			gender="��";
		}
		else if(woman_radioButton.isSelected())
		{
			gender="Ů";
		}
		String department= this.departmentField.getText();
		if(StringUtil.isEmpty(department)) {
			JOptionPane.showMessageDialog(null, "����Ժϵ����Ϊ��");
			return;
		}
		if(password.equals(repeat_password)==false)
		{
			JOptionPane.showMessageDialog(null, "������������Ĳ�һ�£����������룡");
			return;
		}
		reader rea = new reader(account,password,name,gender,department);
		Connection con=null;
		try {
			con = dbUtil.getCon();
			int registerNum= readerDao.register(con, rea);
			if(registerNum==1) {
				JOptionPane.showMessageDialog(null, "ע��ɹ���");
				resetValue();
			}else {
				JOptionPane.showMessageDialog(null, "ע��ʧ�ܣ�");
			}
			
		}catch(Exception e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "ע��ʧ�ܣ�");
		}finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	/**
	 * ���ñ�
	 */
	private void resetValue() {
		this.account_textField.setText("");
		this.name_textField.setText("");
		this.password_Field.setText("");
		this.man_radioButton.setSelected(true);
		this.repeat_password_Field.setText("");
		this.departmentField.setText("");
		}
	
	public  void closeThis() {
		jf.dispose();
	}
	public static void main(String[] args) {
		new registerFrame().init();	
	}
}
